<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
   
  <meta name="keywords" content="The-Lonely-Wandeer,小和尚,blog" />
   
  <meta name="description" content="The-Lonely-Wandrer(小和尚)个人blog" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    关于IE浏览器使用window.location.href的问题优化 |  The-Lonely-Wanderer
  </title>
  <meta name="generator" content="hexo-theme-yilia-plus">
  
  <link rel="shortcut icon" href="/favicon.ico" />
  
  
<link rel="stylesheet" href="/css/main.css">

  
  <script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>
  
  

  

</head>

</html>

<body>
  <div id="app">
    <main class="content">
      <section class="outer">
  <article id="post-关于IE浏览器使用window-location-href的问题优化" class="article article-type-post" itemscope
  itemprop="blogPost" data-scroll-reveal>

  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  关于IE浏览器使用window.location.href的问题优化
</h1>
  

    </header>
    

    
    <div class="article-meta">
      <a href="/2020/04/10/%E5%85%B3%E4%BA%8EIE%E6%B5%8F%E8%A7%88%E5%99%A8%E4%BD%BF%E7%94%A8window-location-href%E7%9A%84%E9%97%AE%E9%A2%98%E4%BC%98%E5%8C%96/" class="article-date">
  <time datetime="2020-04-10T09:03:44.000Z" itemprop="datePublished">2020-04-10</time>
</a>
      
  <div class="article-category">
    <a class="article-category-link" href="/categories/IE-%E4%BC%98%E5%8C%96/">IE 优化</a>
  </div>

      
      
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">354字</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">1分钟</span>
        </span>
    </span>
</div>

      
    </div>
    

    
    
    <div class="tocbot"></div>





    

    
    <div class="article-entry" itemprop="articleBody">
      


      

      
      <h2 id="关于IE浏览器使用window-location-href的问题优化"><a href="#关于IE浏览器使用window-location-href的问题优化" class="headerlink" title="关于IE浏览器使用window.location.href的问题优化"></a>关于IE浏览器使用window.location.href的问题优化</h2><blockquote>
<blockquote>
<p>浏  览  器：IE</p>
</blockquote>
<blockquote>
<p>问题描述：使用window.location.href 路径问题，导致资源访问404</p>
</blockquote>
<blockquote>
<p>备        注：问题只出现在IE浏览器内，其他浏览器正常</p>
</blockquote>
</blockquote>
<a id="more"></a>
<h3 id="1、问题"><a href="#1、问题" class="headerlink" title="1、问题"></a>1、问题</h3><pre><code>JS内使用window.location.href=&quot;资源路径&quot;的方式实现资源访问，因为路径问题导致资源访问404，</code></pre><h3 id="2、原因"><a href="#2、原因" class="headerlink" title="2、原因"></a>2、原因</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">根本原因是IE对资源路径的解析不一样</span><br><span class="line">  <span class="number">1</span>) IE解析路径是以当前页面为根路径</span><br><span class="line">  	<span class="comment">//当前路径</span></span><br><span class="line">  	<span class="comment">//window.location.href = localhost:8081/test/a/b/index.do</span></span><br><span class="line">  	<span class="keyword">var</span> _DataAPI.testHref = <span class="string">"a/b/c.do"</span></span><br><span class="line">  	<span class="built_in">window</span>.location.href = _DataAPI.testHref[参数];</span><br><span class="line">  	<span class="comment">//跳转之后控制台输入window.location.href</span></span><br><span class="line">  	localhost:<span class="number">8081</span>/test/a/b/a/b/c.do</span><br><span class="line">  <span class="number">2</span>) 非IE浏览器以项目根路径为根路径</span><br><span class="line">    <span class="comment">//当前路径</span></span><br><span class="line">  	<span class="comment">//window.location.href = localhost:8081/test/a/b/index.do</span></span><br><span class="line">  	<span class="keyword">var</span> _DataAPI.testHref = <span class="string">"a/b/c.do"</span></span><br><span class="line">  	<span class="built_in">window</span>.location.href = _DataAPI.testHref[参数];</span><br><span class="line">  	<span class="comment">//跳转之后控制台输入window.location.href</span></span><br><span class="line">  	localhost:<span class="number">8081</span>/test/a/b/c.do</span><br></pre></td></tr></table></figure>
<h3 id="3、解决办法"><a href="#3、解决办法" class="headerlink" title="3、解决办法"></a>3、解决办法</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">	问题只出现在使用IE内核的浏览器上，目前解决的办法是针对IE内核的浏览器做优化，</span><br><span class="line">方法：判断浏览器是否为IE浏览器</span><br><span class="line">	<span class="number">1</span>)IE浏览器：不加前缀（/a/b）</span><br><span class="line">	<span class="number">2</span>)非IE浏览器：不做处理</span><br><span class="line">	<span class="comment">//IE浏览器时路径的层级移除重复的部分即可</span></span><br><span class="line">	<span class="keyword">var</span> _DataAPI.testHref = (!!<span class="built_in">window</span>.ActiveXObject ||<span class="string">"ActiveXObject"</span> <span class="keyword">in</span> <span class="built_in">window</span>) ? <span class="string">"c.do"</span>:<span class="string">"a/b/c.do"</span>;</span><br></pre></td></tr></table></figure>



      
      <!-- reward -->
      
    </div>
    
    
      <!-- copyright -->
      
        <div class="declare">
          <ul class="post-copyright">
            <li>
              <i class="ri-copyright-line"></i>
              <strong>版权声明： </strong s>
              本博客所有文章除特别声明外，均采用 <a href="https://www.apache.org/licenses/LICENSE-2.0.html" rel="external nofollow"
                target="_blank">Apache License 2.0</a> 许可协议。转载请注明出处！
            </li>
          </ul>
        </div>
        
    <footer class="article-footer">
      
          
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://the-lonely-wanderer.gitee.io/2020/04/10/%E5%85%B3%E4%BA%8EIE%E6%B5%8F%E8%A7%88%E5%99%A8%E4%BD%BF%E7%94%A8window-location-href%E7%9A%84%E9%97%AE%E9%A2%98%E4%BC%98%E5%8C%96/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/IE%E7%9A%84window-location-href/" rel="tag">IE的window.location.href</a></li></ul>


    </footer>

  </div>

  
  
  <nav class="article-nav">
    
      <a href="/2020/08/28/videoTest/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            videoTest
          
        </div>
      </a>
    
    
      <a href="/2020/03/26/IE-%E6%B5%8F%E8%A7%88%E5%99%A8%E6%97%B6%E5%B7%AE/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">IE 浏览器时差</div>
      </a>
    
  </nav>


  

  
  
<!-- valine评论 -->
<div id="vcomments-box">
    <div id="vcomments">
    </div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src='https://cdn.jsdelivr.net/npm/valine@1.3.10/dist/Valine.min.js'></script>
<script>
    new Valine({
        el: '#vcomments',
        app_id: '',
        app_key: '',
        path: window.location.pathname,
        notify: 'false',
        verify: 'true',
        avatar: 'mp',
        placeholder: '给我的文章加点评论吧~',
        recordIP: true
    });
    const infoEle = document.querySelector('#vcomments .info');
    if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
        infoEle.childNodes.forEach(function (item) {
            item.parentNode.removeChild(item);
        });
    }
</script>
<style>
    #vcomments-box {
        padding: 5px 30px;
    }

    @media screen and (max-width: 800px) {
        #vcomments-box {
            padding: 5px 0px;
        }
    }

    #vcomments-box #vcomments {
        background-color: #fff;
    }

    .v .vlist .vcard .vh {
        padding-right: 20px;
    }

    .v .vlist .vcard {
        padding-left: 10px;
    }
</style>

  

  
  
<div class="gitalk" id="gitalk-container"></div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1.6.2/dist/gitalk.css">


<script src="https://cdn.jsdelivr.net/npm/gitalk@1.6.2/dist/gitalk.min.js"></script>


<script src="https://cdn.jsdelivr.net/npm/blueimp-md5@2.10.0/js/md5.min.js"></script>

<script type="text/javascript">
  var gitalk = new Gitalk({
    clientID: '371faa36e8d4f35ab922',
    clientSecret: 'd8fb7d8f4554ce72d078243c4924d58c30bc94d5',
    repo: 'The-Lonely-Wanderer.github.io',
    owner: 'The-Lonely-Wanderer',
    admin: ['The-Lonely-Wanderer'],
    // id: location.pathname,      // Ensure uniqueness and length less than 50
    id: md5(location.pathname),
    distractionFreeMode: false,  // Facebook-like distraction free mode
    pagerDirection: 'last'
  })

  gitalk.render('gitalk-container')
</script>

  

</article>
</section>
      <footer class="footer">
  <div class="outer">
    <ul class="list-inline">
      <li>
        &copy;
        2015-2020
        小和尚
      </li>
      <li>
        
        Powered by
        
        
        <a href="https://hexo.io" target="_blank">Hexo</a> Theme <a href="https://github.com/Shen-Yu/hexo-theme-ayer" target="_blank">Ayer</a>
        
      </li>
    </ul>
    <ul class="list-inline">
      <li>
        
        
        <span>
  <i>PV:<span id="busuanzi_value_page_pv"></span></i>
  <i>UV:<span id="busuanzi_value_site_uv"></span></i>
</span>
        
      </li>
      
      <li>
        <!-- cnzz统计 -->
        
      </li>
    </ul>
  </div>
</footer>
      <div class="to_top">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>
      </div>
    </main>
    <aside class="sidebar">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/avatar.jpg" alt="The-Lonely-Wanderer"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/2019/about">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i></p>
  <div class="reward-box">
    
    
  </div>
</div>
    
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/share.js"></script>


<script src="/js/lazyload.min.js"></script>


<script>
  try {
    var typed = new Typed("#subtitle", {
      strings: ['面朝大海，春暖花开', '愿你一生努力，一生被爱', '想要的都拥有，得不到的都释怀'],
      startDelay: 0,
      typeSpeed: 200,
      loop: true,
      backSpeed: 100,
      showCursor: true
    });
  } catch (err) {
  }

</script>




<script src="/js/tocbot.min.js"></script>

<script>
  // Tocbot_v4.7.0  http://tscanlin.github.io/tocbot/
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto',
    onClick: (e) => {
      $('.toc-link').removeClass('is-active-link');
      $(`a[href=${e.target.hash}]`).addClass('is-active-link');
      $(e.target.hash).scrollIntoView();
      return false;
    }
  });
</script>



<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/js/ayer.js"></script>



<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>




<script src="/js/busuanzi-2.3.pure.min.js"></script>



<script type="text/javascript" src="https://js.users.51.la/20544303.js"></script>


<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.2/dist/jquery.fancybox.min.css">
<script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.2/dist/jquery.fancybox.min.js"></script>


    
    
<style>
    #music {
        position: fixed;
        right: 15px;
        bottom:-175px;
        z-index: 998;
        transition:0.5s;
    }
</style>

<div id="music">
    
    
    
    <iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width="300" height="270" 
        src="//music.163.com/outchain/player?type=0&id=2455677284&auto=1&height=430"></iframe>
</div>

<script type="text/javascript">
    var music = document.getElementById("music");
    music.onmouseenter=function(){
        this.style.bottom="0px";
    }
    music.onmouseleave=function(){
        this.style.bottom="-175px";
    }
    window.onload =function(){
        console.clear();
        console.log("                          _ooOoo_                              "+"\n "+
                    "                         o8888888o                             "+"\n "+
                    "                         88 "+" . "+" 88                       "+"\n "+
                    "                         (| ^_^ |)                             "+"\n "+
                    "                         O\  =  /O                             "+"\n "+
                    "                      ____/`---'\____                          "+"\n "+
                    "                    .'  \\|     |//  `.                        "+"\n "+
                    "                   /  \\|||  :  |||//  \                       "+"\n "+
                    "                  /  _||||| -:- |||||-  \                      "+"\n "+
                    "                  |   | \\\  -  /// |   |                      "+"\n "+
                    "                  | \_|  ''\---/''  |   |                      "+"\n "+
                    "                  \  .-\__  `-`  ___/-. /                      "+"\n "+
                    "                ___`. .'  /--.--\  `. . ___                    "+"\n "+
                    "              .`` '<  `.___\_<|>_/___.'  >'``.                 "+"\n "+
                    "            | | :  `- \`.;`\ _ /`;.`/ - ` : | |                "+"\n "+
                    "            \  \ `-.   \_ __\ /__ _/   .-` /  /                "+"\n "+
                    "      ========`-.____`-.___\_____/___.-`____.-'========        "+"\n "+
                    "                           `=---='                             "+"\n "+
                    "|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"+"\n "+
                    "||             佛祖保佑       永不宕机      永无BUG            ||"+"\n "+
                    "|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
                            );
    }
</script>
    
  </div>
</body>

</html>